﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.Office.Server.Search.Administration;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace SearchAdminToolkit.UI.Controls
{
    /// <summary>
    /// Defines a control for displaying the ranking status information.
    /// </summary>
    public class RankingStatusToolkitControl : BaseToolkitControl, INamingContainer
    {

        #region Fields

        private System.Web.UI.WebControls.LinkButton _startRankingUpdateLinkButton;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the contained link button to start a ranking update.
        /// </summary>
        private LinkButton StartRankingUpdateLinkButton
        {
            get
            {
                if (_startRankingUpdateLinkButton == null)
                {
                    _startRankingUpdateLinkButton = new LinkButton();
                    _startRankingUpdateLinkButton.Text = "Start Ranking Update";
                }

                return _startRankingUpdateLinkButton;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the section description for the toolkit control.
        /// </summary>
        /// <returns></returns>
        protected override string GetSectionDescription()
        {
            return "";
        }

        /// <summary>
        /// Gets the section title for the toolkit control.
        /// </summary>
        /// <returns></returns>
        protected override string GetSectionTitle()
        {
            return "Ranking Status";
        }

        /// <summary>
        /// Overridden to create the composite toolkit control.
        /// </summary>
        protected override void CreateChildControls()
        {
            this.StartRankingUpdateLinkButton.Click += new EventHandler(StartRankingUpdateLinkButton_Click);
            this.Controls.Add(this.StartRankingUpdateLinkButton);

            base.CreateChildControls();
        }

        /// <summary>
        /// Renders the composite toolkit control.
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderToolkitControl(System.Web.UI.HtmlTextWriter writer)
        {
            Ranking rank = RankingManager.GetRanking();

            // <br />
            writer.RenderBeginTag(HtmlTextWriterTag.Br);
            writer.RenderEndTag();

            // <table>
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            this.RenderNameValueRow(writer, "Current status", rank.Status.ToString());
            this.RenderNameValueRow(writer, "Last update start", rank.LastUpdateStart.ToString());
            this.RenderNameValueRow(writer, "Last update end", rank.LastUpdateEnd.ToString());

            // </table>
            writer.RenderEndTag();

            writer.AddAttribute(HtmlTextWriterAttribute.Src, "/_layouts/images/rect.gif");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.Write("&nbsp;&nbsp;");
            this.StartRankingUpdateLinkButton.RenderControl(writer);
            
        }

        /// <summary>
        /// Renders a name / value pair TR.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        private void RenderNameValueRow(HtmlTextWriter writer, string name, string value)
        {
            // <tr>
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            // <th>
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-formlabel");
            writer.RenderBeginTag(HtmlTextWriterTag.Th);
            writer.Write(name + ":");

            // </th>
            writer.RenderEndTag();

            // <td>
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-propertysheet");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(value);

            // </td>
            writer.RenderEndTag();

            // </tr>
            writer.RenderEndTag();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Responds to the event raised when the start ranking link is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void StartRankingUpdateLinkButton_Click(object sender, EventArgs e)
        {
            RankingManager.StartRankingUpdate();
        }

        #endregion

    }
}
